//
//  KSFinishRegisterViewController.swift
//  Merchant
//
//  Created by liuxm on 2017/8/29.
//  Copyright © 2017年 huanniu. All rights reserved.
//

import UIKit

/// 完成注册
class KSFinishRegisterViewController: KSBaseViewController {
    /// 输入密码
    @IBOutlet weak var passwordTf: UITextField!
    /// 确认密码
    @IBOutlet weak var confimPasswordTf: UITextField!
    /// 支付密码
    @IBOutlet weak var payPasswordTf: UITextField!
    /// 注册
    @IBOutlet weak var registerBtn: UIButton!
    
    var userInfo: [String: Any]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.title = "注册"
        
        self.registerBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorMainRed), for: UIControlState.normal)
        self.registerBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonHighlight), for: UIControlState.highlighted)
        self.registerBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonDisabled), for: UIControlState.disabled)
    }
    
    // 密码
    @IBAction func passwordDidChanged(_ sender: Any) {
        self.checkRegisterBtnStatus()
    }
    
    // 确认密码
    @IBAction func confimPasswordDidChanged(_ sender: Any) {
        self.checkRegisterBtnStatus()
    }
    
    // 支付密码
    @IBAction func payPasswordDidChanged(_ sender: Any) {
        self.checkRegisterBtnStatus()
    }
    
    private func checkRegisterBtnStatus() {
        if (self.passwordTf.text?.isEmpty)! || (self.confimPasswordTf.text?.isEmpty)! || (self.payPasswordTf.text?.isEmpty)! {
            self.registerBtn.isEnabled = false
        } else {
            self.registerBtn.isEnabled = true
        }
    }
    
    // 注册
    @IBAction func registerBtnClick(_ sender: Any) {
        if (self.passwordTf.text?.contains(" "))! {
            progressHUD.showMessage(text: "登录密码不能包含空格", duration: 2, animated: true)
        } else {
            if self.passwordTf.text != self.confimPasswordTf.text {
                progressHUD.showMessage(text: "密码不一致，请重新输入", duration: 2, animated: true)
            } else {
                if (self.payPasswordTf.text! as NSString).length != 6 {
                    progressHUD.showMessage(text: "请输入6位数字支付密码", duration: 2, animated: true)
                } else {
                    self.startLoading()
                    let params: [String: Any] = ["password": self.passwordTf.text!, "pay_password": self.payPasswordTf.text!]
                    let dict:NSMutableDictionary = NSMutableDictionary(dictionary: params)
                    dict.addEntries(from: self.userInfo)
                    
                    request.requestRegister(parameters: dict as! [String : Any])
                }
            }
        }
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        if apiType == KSApiType.KSRegister {
            progressHUD.showMessage(text: "注册成功", duration: 2, animated: true)
            let delay = DispatchTime.now() + DispatchTimeInterval.seconds(2)
            DispatchQueue.main.asyncAfter(deadline: delay, execute: {
                self.navigationController?.popToRootViewController(animated: true)
            })
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.passwordTf.resignFirstResponder()
        self.confimPasswordTf.resignFirstResponder()
        self.payPasswordTf.resignFirstResponder()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
